#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>
#include <sstream>
#include <list>
using namespace std;


struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
 
class Solution {
public:
    int threeSumClosest(vector<int>& nums , int target) {
        //升序排序
        sort(nums.begin(),nums.end());
        int n = nums.size();
        int best = 1e7;
        //根据差值的绝对值来更新答案
        auto update = [&](int cur) {
            if(abs(cur - target) < abs(best - target)) best = cur;
        };

        //枚举a
        for( int i=0;i < n;++i) {
            if(i > 0 && nums[i] == nums[i-1]) continue; //保证和上一次枚举的元素不同
            //使用双指针枚举b和c
            int j = i + 1,k = n - 1;
            while(j < k) {
                int sum = nums[i] + nums[j] + nums[k];
                //如果sum为target返回答案
                if( sum == target ) return target;
                update(sum);
                //找到下一个满足条件的位置
                if( sum > target ) {
                    int k0 = k - 1;
                    while( j < k0 && nums[k0] == nums[k] ) --k0; //移动到下一个不相等的元素处
                    k = k0;
                } else {
                    int j0 = j + 1;
                    while( k > j0 && nums[j0] == nums[j]) ++j0;
                    j = j0;
                }
            }
        }
        return best;
    }

};

void func(void ) {
    int cnt = 0;

    auto addAuto = [&] {
        cnt = cnt + 1;
        cout << "hello " << cnt<<endl;
    };

    for (int i = 0 ;i<3;i++) {
        addAuto(cnt);
    }
}
int main(){

    func();
    return 0;
}